<?PHP
  class PageListWidget extends WidgetBase{
    public function load(){
      $dir = '';
      if(isset($_GET['dir']) &&  substr($_GET['dir'],0,1) != '.'){
        $dir = $_GET['dir'];
      }

      $this->headline = $GLOBALS['language']->getString("PAGES");

      if(Cache::contains("page","widget_".$_GET['dir'])){
        $this->content = Cache::getData("page","widget_".$_GET['dir']);
      }
      else{
        $articlepath =Settings::getInstance()->get("root")."content/articles/";
        $template = new Template();
        $template->load("plugin_pagelistwidget_pagelist");
        $template->assign_var("FORMURL","/admin/index.php?page=sites&dir=".urlencode($dir));
 
        if(isset($_POST['newFolder'])){
          FileServer::createFolder($articlepath.$dir,$_POST['name']);
        }
        if(isset($_GET['rmdir'])){
          rmdir($articlepath.$_GET['rmdir']);
          $template->assign_var("MESSAGE","Verzeichnis ".$_GET['rmdir']." wurde gel&ouml;scht!");
        }
        else{
          $template->assign_var("MESSAGE","");
        }
  
        $verzeichnis = openDir($articlepath.$dir);
 
        if($dir){
          $template->assign_var("NEWPAGELINK","<a href=\"/admin/index.php?page=site-new&dir=".substr($dir,1)."\">Neue Seite</a>");
        }
        else{
          $template->assign_var("NEWPAGELINK"," <a href=\"/admin/index.php?page=site-new\">Neue Seite</a>");
        }
        $pre = "";
        $path = "";
        foreach(explode("/",$dir) as $cDir){
          $index = $template->add_loop_item("PATH");
          $path .= "/".$cDir;
          if($path == "/"){
            $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=sites");
            $template->assign_loop_var("PATH", $index, "LABEL","/");
            $path = "";
          }
          else{
            $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=sites&dir=".$path);
            $template->assign_loop_var("PATH", $index, "LABEL",$cDir);
          }
        }
        $subFolders = FileServer::getFolders($articlepath.$dir);
        if($subFolders){
          foreach($subFolders as $folder){
            $index = $template->add_loop_item("DIRS");
            $template->assign_loop_var("DIRS",$index,"URL","/admin/index.php?page=sites&dir=".$dir."/".$folder);
            $template->assign_loop_var("DIRS",$index,"LABEL",$folder);
          }
        }
        if(trim($dir) != "" & trim($dir) != "/"){
          $template->assign_var("DELETEFOLDERLINK","<a href=\"/admin/index.php?page=sites&rmdir=".$_GET['dir']."\">Ordner l&ouml;schen</a>");
        }
        else{
          $template->assign_var("DELETEFOLDERLINK","");
        }
        $pages = Page::getPagesByDir($dir);
        if(substr($dir,0,1) == "/"){
          $shortdir = substr($dir,1);
        }
        else{
          $shortdir = $dir;
        }
        if($pages){
          foreach($pages as $page){
            $shortalias = substr($page->alias,strlen($shortdir));
            if(substr($shortalias,0,1) == "/"){
              $shortalias = substr($shortalias,1);
            }
            else{
              $shortalias = $shortalias;
            }
            if(!strpos($shortalias,"/")){
              $host = Settings::getInstance()->get("host");
              $index = $template->add_loop_item("PAGES");
              $template->assign_loop_var("PAGES",$index,"TITLE",$page->title);
              $template->assign_loop_var("PAGES",$index,"ALIAS",$shortalias);
              $template->assign_loop_var("PAGES",$index,"EDITURL",$host."admin/pageedit.html?site=".$page->alias);
              $template->assign_loop_var("PAGES",$index,"DELETEURL",$host."admin/pagedelete.html?site=".$page->alias);
            }
          }
          $template->assign_var("NOPAGES","");
        }
        else{
         $template->assign_var("NOPAGES","Es sind keine Seiten vorhanden.");
        }
        $this->content = $template->getCode();
        Cache::setData("page","widget_".$_GET['dir'],$this->content);
      }
    }
  }
?>